package com.yj.suncity.collect;

import java.util.List;

import org.json.JSONObject;

import com.yj.suncity.R;
import com.yj.suncity.activity.shouye.SeeAdvDetailActivity;
import com.yj.suncity.async.AsyncTaskUtil;
import com.yj.suncity.async.AsyncTaskUtil.ResultCallBack;
import com.yj.suncity.collect.bean.CollectAdvBean;
import com.yj.suncity.collect.bean.CollectListBean;
import com.yj.suncity.constant.URLCon;
import com.yj.suncity.utis.DialogUtils;
import com.yj.suncity.utis.ToastUtil;
import com.yj.suncity.utis.DialogUtils.ReshActivity;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class CollectAdvListActivity extends Activity {
	private Context context;
	private String userId;
	private DisplayImageOptions options;
	private ImageLoader imageLoader;
	private ListView mList_collect_view;
	private int flag;// 1,收藏广告-》当前 2，收藏信息-》当前

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_collect_list);
		context = this;
		userId = getIntent().getStringExtra("userid");
		flag = getIntent().getIntExtra("flag", -1);
		options = new DisplayImageOptions.Builder()
				.showStubImage(R.drawable.android_layout_detail)
				.showImageForEmptyUri(R.drawable.android_layout_detail)
				.showImageOnFail(R.drawable.android_layout_detail)
				.cacheInMemory(true).cacheOnDisc(true)
				// .displayer(new RoundedBitmapDisplayer(10))// 设置成圆角图片
				.bitmapConfig(Bitmap.Config.RGB_565) // 设置图片的解码类型
				.build();
		findView();
		initData();
	}

	private void findView() {
		if (flag == 2) {// 信息收藏
			((TextView) findViewById(R.id.txt_title_name)).setText("收藏信息");
		} else {
			((TextView) findViewById(R.id.txt_title_name)).setText("收藏广告");
		}
		TextView emptyView = (TextView) findViewById(R.id.txt_empty);
		mList_collect_view = (ListView) findViewById(R.id.list_collect_view);
		mList_collect_view.setEmptyView(emptyView);
	}

	private void initData() {
		String path = null;
		if (flag == 2) {
			path = URLCon.收藏 + "?action=getentcollectionlist&userid=" + userId;
		} else {
			path = URLCon.收藏 + "?action=getadvcollectionlist&userid=" + userId;
		}
		Log.d("", path);
		AsyncTaskUtil asyncTaskUtil = new AsyncTaskUtil(context, null, null,
				false, null, true, "正在加载......", new ResultCallBack() {
					@Override
					public void JSONResult(JSONObject jsonObject) {
						CollectListBean listBean = new CollectListBean(
								jsonObject);
						mList_collect_view.setAdapter(new CollectAdvAdapter(
								listBean.getAdvData()));

					}
				});
		asyncTaskUtil.execute(path);
	}

	class CollectAdvAdapter extends BaseAdapter {
		private List<CollectAdvBean> advBeans;

		public CollectAdvAdapter(List<CollectAdvBean> advBeans) {
			super();
			imageLoader = ImageLoader.getInstance();
			this.advBeans = advBeans;
		}

		@Override
		public int getCount() {
			return advBeans.size();
		}

		@Override
		public Object getItem(int position) {
			return advBeans.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = LayoutInflater.from(context).inflate(
						R.layout.adapter_collect_adv_list, null);
				holder.mImg_logo = (ImageView) convertView
						.findViewById(R.id.img_logo);
				holder.mTxt_name = (TextView) convertView
						.findViewById(R.id.txt_shop_name);
				holder.mTxt_jianjie = (TextView) convertView
						.findViewById(R.id.txt_shop_jianjie);
				holder.mTxt_all_totle = (TextView) convertView
						.findViewById(R.id.txt_all_num);
				holder.mTxt_give_num = (TextView) convertView
						.findViewById(R.id.txt_give_num);
				holder.mLay_linear_adv_item = (LinearLayout) convertView
						.findViewById(R.id.lay_linear_adv_item);

				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			final CollectAdvBean advBean = advBeans.get(position);
			holder.mTxt_name.setText(advBean.getEntname());
			holder.mTxt_jianjie.setText(advBean.getEnt_jianjie());
			holder.mTxt_all_totle.setText(advBean.getGold_total());
			holder.mTxt_give_num.setText(advBean.getGive_num());
			if (!advBean.getEnt_logo().equals("")) {
				imageLoader.displayImage(URLCon.HOST + advBean.getEnt_logo(),
						holder.mImg_logo, options);
			} else {
				holder.mImg_logo
						.setImageResource(R.drawable.android_layout_detail);
			}
			holder.mLay_linear_adv_item
					.setOnClickListener(new OnClickListener() {
						@Override
						public void onClick(View v) {

							Intent intent = new Intent(context,
									SeeAdvDetailActivity.class);
							intent.putExtra("adv_userid", advBean.getUserid());
							intent.putExtra("advid", advBean.getAid());
							intent.putExtra("flag", flag);
							startActivityForResult(intent, 1);
						}
					});
			holder.mLay_linear_adv_item
					.setOnLongClickListener(new OnLongClickListener() {
						@Override
						public boolean onLongClick(View v) {
							DialogUtils dialogUtils = DialogUtils.getInstance();
							dialogUtils.backDialog(context, "确定删除吗？", true,
									new ReshActivity() {
										@Override
										public void reshActivity() {
											if (flag == 2) {// 信息收藏
												deleteCollect(
														advBean.getUserid(), 1);
											} else {
												deleteCollect(advBean.getAid(),
														2);
											}
										}
									});
							return false;
						}
					});
			return convertView;
		}
	}

	class ViewHolder {
		ImageView mImg_logo;
		TextView mTxt_name;
		TextView mTxt_jianjie;
		TextView mTxt_all_totle;
		TextView mTxt_give_num;
		LinearLayout mLay_linear_adv_item;
	}

	private void deleteCollect(String collectid, int type) {
		String path = URLCon.收藏 + "?action=delcollection&userid=" + userId
				+ "&id=" + collectid + "&type=" + type;
		Log.d("", "删除：" + path);
		AsyncTaskUtil asyncTaskUtil = new AsyncTaskUtil(context, null, null,
				false, null, true, "正在删除......", new ResultCallBack() {

					@Override
					public void JSONResult(JSONObject jsonObject) {
						if (jsonObject.optString("success").equals("1")) {
							initData();
						} else {
							ToastUtil.ShowToast(context,
									"" + jsonObject.optString("msg"));
						}
					}
				});
		asyncTaskUtil.execute(path);
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnHeadBack:
			this.finish();
			break;
		default:
			break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == 1 && resultCode == 1) {
			initData();
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

}
